﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pavilion.Communications;

namespace Pavilion.DesignerTool
{
    static public class GameToolTransfer
    {
        static public void Add(LoadNodeWrapper loadNodeWrapper, GameItemWrapper gameItemWrapper, string gameToolName)
        {
            var resourceName = loadNodeWrapper.NodeScriptWrapper.ResourceData.ResourceName;
            var scriptName = loadNodeWrapper.NodeScriptWrapper.Name;
            var containerNodeNameList = NodeTransfer.CreateNodeStringCollection(loadNodeWrapper.ParentContainerNodeWrapper);
            var gameToolData = gameItemWrapper.ObtainGameTool(gameToolName);

            Add(resourceName, scriptName, (gameToolData.NodeWrapperParent as LoadNodeWrapper).LoadNode, gameToolData.GameData);
        }

        static public void Add(string resourceName, string scriptName, LoadNode loadNode, GameData gameToolData)
        {
            var containerNodeNameList = NodeTransfer.CreateNodeStringCollection(loadNode.ParentContainer);
            DataTransferClient.Instance.TransferData((proxy) => proxy.AddGameTool(resourceName, scriptName, containerNodeNameList, loadNode.Name,
                gameToolData.GameItemName, gameToolData.GameToolName));

            foreach (string property in gameToolData.PropertyDictionary.Keys)
                AddGameToolProperty(resourceName, scriptName, loadNode, gameToolData, property);
        }

        static public void Remove(LoadNodeWrapper loadNodeWrapper, GameItemWrapper gameItemWrapper, string gameToolName)
        {
            var resourceName = loadNodeWrapper.NodeScriptWrapper.ResourceData.ResourceName;
            var scriptName = loadNodeWrapper.NodeScriptWrapper.Name;

            Remove(resourceName, scriptName, loadNodeWrapper.LoadNode, gameItemWrapper.Name, gameToolName);
        }

        static public void Remove(string resourceName, string scriptName, LoadNode loadNode, string gameItemName, string gameToolName)
        {
            var containerNodeNameList = NodeTransfer.CreateNodeStringCollection(loadNode.ParentContainer);

            DataTransferClient.Instance.TransferData((proxy) => proxy.RemoveGameTool(resourceName, scriptName, containerNodeNameList, loadNode.Name,
                gameItemName, gameToolName));
        }

        static public void AddGameToolProperty(GameDataWrapper gameDataWrapper, string propertyName)
        {
            string resourceName = gameDataWrapper.NodeWrapperParent.NodeScriptWrapper.ResourceData.ResourceName;
            string scriptName = gameDataWrapper.NodeWrapperParent.NodeScriptWrapper.Name;

            AddGameToolProperty(resourceName, scriptName, (gameDataWrapper.NodeWrapperParent as LoadNodeWrapper).LoadNode, gameDataWrapper.GameData, propertyName);
        }

        static public void AddGameToolProperty(string resourceName, string scriptName, LoadNode loadNode, GameData gameToolData, string propertyName)
        {
            var containerNodeNameList = NodeTransfer.CreateNodeStringCollection(loadNode.ParentContainer);
            var propertyValue = gameToolData.PropertyDictionary[propertyName];
            var propertyStringValue = GameEngine.GetStringRepresention(propertyValue);
            var assemblyName = propertyValue.GetType().Assembly.ManifestModule.FullyQualifiedName;
            var propertyTypeName = propertyValue.GetType().FullName;

            DataTransferClient.Instance.TransferData((proxy) => proxy.AddGameToolDataProperty(resourceName, scriptName, containerNodeNameList, loadNode.Name,
                gameToolData.GameItemName, gameToolData.GameToolName, propertyName, propertyStringValue, assemblyName, propertyTypeName));
        }

        static public void RemoveGameToolProperty(GameDataWrapper gameDataWrapper, string propertyName)
        {
            string resourceName = gameDataWrapper.NodeWrapperParent.NodeScriptWrapper.ResourceData.ResourceName;
            string scriptName = gameDataWrapper.NodeWrapperParent.NodeScriptWrapper.Name;
            
            RemoveGameToolProperty(resourceName, scriptName, gameDataWrapper.NodeWrapperParent.Node as LoadNode, gameDataWrapper.GameData, propertyName);
        }

        static public void RemoveGameToolProperty(string resourceName, string scriptName, LoadNode loadNode, GameData gameToolData, string propertyName)
        {
            var containerNodeNameList = NodeTransfer.CreateNodeStringCollection(loadNode.ParentContainer);

            DataTransferClient.Instance.TransferData((proxy) => proxy.RemoveGameToolDataProperty(resourceName, scriptName, containerNodeNameList, loadNode.Name,
                gameToolData.GameItemName, gameToolData.GameToolName, propertyName));
        }

        static public void UpdateGameToolProperty(GameDataWrapper gameDataWrapper, string propertyName)
        {
            var resourceName = gameDataWrapper.NodeWrapperParent.NodeScriptWrapper.ResourceData.ResourceName;
            var scriptName = gameDataWrapper.NodeWrapperParent.NodeScriptWrapper.Name;

            UpdateGameToolProperty(resourceName, scriptName, gameDataWrapper.NodeWrapperParent.Node as LoadNode, gameDataWrapper.GameData, propertyName);
        }

        static public void UpdateGameToolProperty(string resourceName, string scriptName, LoadNode loadNode, GameData gameToolData, string propertyName)
        {
            var propertyValue = gameToolData.PropertyDictionary[propertyName];
            var containerNodeNameList = NodeTransfer.CreateNodeStringCollection(loadNode.ParentContainer);
            var propertyStringValue = GameEngine.GetStringRepresention(propertyValue);
            var assemblyName = propertyValue.GetType().Assembly.ManifestModule.FullyQualifiedName;
            var propertyTypeName = propertyValue.GetType().FullName;

            DataTransferClient.Instance.TransferData((proxy) => proxy.UpdateGameToolDataProperty(resourceName, scriptName, containerNodeNameList, loadNode.Name,
                gameToolData.GameItemName, gameToolData.GameToolName, propertyName, propertyStringValue, assemblyName, propertyTypeName));
        }
    }
}
